import db.MYSQLControl;
import driver.PhantomJsDriver;
import driver.chromedriver;
import model.model;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import parse.priceParse;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class test_old {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = null;

        try {

            driver = chromedriver.getChromeDriver();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //driver = PhantomJsDriver.getPhantomJSDriver();
        driver.get("http://car.bitauto.com/tree_chexing/mb_9/");
        (new WebDriverWait( driver, 10)) .until(
                ExpectedConditions.presenceOfElementLocated(By.xpath(String.format("//span[text()='%s']","大众")))
        ).click();

        WebElement element = driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/div/a/img", 1, 1)));
        Actions actions = new Actions(driver);
        actions.moveToElement(element).click().perform();

        Set<String> all_handles = driver.getWindowHandles();
        Iterator<String> it = all_handles.iterator();
        String current_handles = driver.getWindowHandle();

        while (it.hasNext()) {
            if (current_handles == it.next()) continue;

//跳入新窗口
            WebDriver window = driver.switchTo().window(it.next());
            WebElement element1 = driver.findElement(By.xpath("//*[contains(text(),\"停售年款\")]"));

            Thread.sleep(1000);
            actions.moveToElement(element1).click().perform();

            List<WebElement> search_result11 = driver.findElements(By.xpath("//*[contains(text(),\"停售年款\")]/../div/a"));



            for(int i_stop = 0;i_stop<search_result11.size();i_stop++){
                element1 = driver.findElement(By.xpath("//*[contains(text(),\"停售年款\")]"));
                actions.moveToElement(element1).click().perform();
                search_result11 = driver.findElements(By.xpath("//*[contains(text(),\"停售年款\")]/../div/a"));
                    actions.moveToElement(search_result11.get(i_stop)).click().perform();
                    (new WebDriverWait( driver, 10)) .until(
                            ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"car_tag\"]/nav/div/div/ul/li[2]/a"))
                    ).click();
                Thread.sleep(2000);


                }








            //driver.findElement(By.xpath("//*[@id=\"car_tag\"]/nav/div/div/ul/li[2]/a")).click();
            Thread.sleep(1000);
            //MYSQLControl.inserCarRecall(priceParse.getData(driver.getPageSource()));

            //driver.close();
        }
    }
}
